home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12663 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.0 KB

  1. Path: mayne.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Rounding a double
  5. Date: 1 Apr 1996 19:12:05 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4jq5u5INN80u@mayne.ugrad.cs.ubc.ca>
  8. References: <4jprh2$1li@newsgate.dircon.co.uk>
  9. NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
  10.  
  11. In article <4jprh2$1li@newsgate.dircon.co.uk>,
  12. Peter Smith <pg-smith@dircon.co.uk> wrote:
  13.  >Has anybody got a C function to round a double to a number of decimal 
  14.  >places that:
  15.  >a) is readable
  16.  >b) is relatively short
  17.  >c) works !!
  18.  >I had a function but it failed if the number of decimal places was zero,
  19.  >if the double was very small,  .. . etc etc.
  20.  >
  21.  >Is there a library of freeware C functions anywhere on the Internet ??
  22.  >
  23.  >Many thanks
  24.  >Pete
  25.  >
  26.  >replies to : pg-smith@dircon.co.uk
  27.  >
  28.  
  29. double round(double x, int digits)
  30.  
  31. {
  32.         char buffer[20];
  33.         sprintf(buffer,"%8.8f",x);
  34.  
  35.         buffer >>= (8 - digits);
  36.  
  37.         return atof(buffer);
  38. }
  39. -- 
  40.  
  41.